package com.yzy.fly.entity.role;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.yzy.fly.jarPackage.ClassFileAccess;
import com.yzy.fly.kernel.model.ability.tree.Tree;
import com.yzy.fly.type.FileType;
import com.yzy.fly.util.BeanUtil;
import com.yzy.fly.util.DateUtil;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
@Data
@TableName("fly_model")
public class FlyModel implements Serializable,Tree<Integer,FlyModel>,ClassFileAccess {

    @TableId(type = IdType.AUTO)
    private Integer id;
//    @JSONField(name="label")
    private Integer seq;
    private String label;
    private String name;
    private String globalKey;
    @TableField(exist=false)
    private String jsonPath;
    private String type;
    private Integer classId;

    private String isArray;
    private Boolean lazy = true;
    @TableField(exist=false)
    private String tableName;
    private Integer parentId;
    private Integer defModelId;
//    private Integer parentFieldId;
//    private String parentFieldName;
    private Integer systemId;

    @TableField(exist=false)
    private boolean open=true;
    private String descr;
    @JSONField(name="children")
    @TableField(exist=false)
    private List<FlyModel> childrens;
    @TableField(exist=false)
    private List<FlyAttribute> attrs;
    @TableField(exist=false)
    private List<FlyAttribute> unionAttrs;

    @DateTimeFormat(pattern= DateUtil.DateFormatPattern.yyyy_MM_dd_HH_mm_ss)
    @JsonFormat( pattern = DateUtil.DateFormatPattern.yyyy_MM_dd_HH_mm_ss )
    private Date createTime;
    @DateTimeFormat(pattern= DateUtil.DateFormatPattern.yyyy_MM_dd_HH_mm_ss)
    @JsonFormat( pattern = DateUtil.DateFormatPattern.yyyy_MM_dd_HH_mm_ss )
    private Date updateTime;

    private String version;
    private int access;
    private String packageName;
    private String className;
    private String classSimpleName;
    private String packageSuperName;
    private String superName;
    @TableField(exist=false)
    private String[] interfaces;

    @TableField(exist=false)
    private FlyTable table;
    @TableField(exist=false)
    private FlyModelWidget widget;
    /**
     * 方法说明:获取实体类名称
     */
//    public String getEntityClazz() {
//        int index = classPath.lastIndexOf('.');
//        return classPath.substring(index + 1);
//    }

    public FlyModel copy(){
        return copy(this);
    }

    public FlyModel copy(FlyModel oriFlyModel){
        FlyModel flyModel = BeanUtil.deepCloneObject(oriFlyModel, FlyModel.class);
        return flyModel;
    }

    public FlyModel copy(FlyDefModel flyDefModel) {
        FlyModel copy = copy();
//        copy.setParentId(flyDefModel.getParentId());
        copy.setDefModelId(flyDefModel.getId());
        copy.setSystemId(flyDefModel.getSystemId());
        return copy;
    }


    @Override
    public boolean isRootNode() {
        if (parentId == null || parentId.intValue() == 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean isParentNode(Integer parentId) {
        if (parentId != null && id != null &&  parentId.intValue() == id.intValue()) {
            return true;
        }
        return false;
    }

    @Override
    public boolean isChildNode(FlyModel parent) {
        if(parentId.intValue() == parent.getId().intValue()) {
            return true;
        }
        return false;
    }

    @Override
    public String getFileName() {
        return getClassName();
    }

    @Override
    public String getFileSuffix() {
        return FileType.CLASS.getValue();
    }

    @Override
    public String getFilePath() {
        return (getPackageName()+ "."+ getClassName()).replace(".","/");
    }

    @Override
    public String getFileFullPath() {
        return getFilePath() + getFileSuffix();
    }

}
